﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.Binaria.SISCOB.Web.Presenter;
using Com.Binaria.SISCOB.Funcional.DTO;

namespace Com.Binaria.SISCOB.Web.Paginas.Mantto
{
    public partial class Default : System.Web.UI.Page
    {

      bool IsView
      {
         get { return !string.IsNullOrEmpty(Request.QueryString["View"]); }
      }

      protected void Page_Load(object sender, EventArgs e)
      {
         if (IsView) this.pnlView.Visible = false;
      }

      Control GetControl()
      {
         //Se pretende Editar y/o Crear
         if (!string.IsNullOrEmpty(Request.QueryString["Id"])
             || (!string.IsNullOrEmpty(Request.QueryString["Action"]) && Request.QueryString["Action"] == "3"))
            Page.MasterPageFile = "~/Paginas/Empty.master";

         return GetControlPlace("cpContenido");
      }

      Control GetControlPlace(string cpName)
      {
         Control cpControl = Page.FindControl(cpName);
         if (cpControl == null)
         {
            MasterPage theMaster = Page.Master;
            while (Page.Master != null)
            {
               cpControl = theMaster.FindControl(cpName);
               if (cpControl != null)
                  return cpControl; // cpControl.Controls.Add(oControl);
               theMaster = theMaster.Master;
            }
         }
         return cpControl;
      }

      protected override void OnPreInit(EventArgs e)
      {
          if (!IsView) return;
          string sView = Request.QueryString["View"];
          //
          Control parentControl = GetControl();
          //
          if (sView == "Cartola")
          {
             ManttoPresenter<CartolaFuncionalDTO> oPresenter = new ManttoPresenter<CartolaFuncionalDTO>();
             oPresenter.Initialize(this);
             oPresenter.RenderView(parentControl);
          }
          else if (sView == "CartolaMayor")
          {
             ManttoPresenter<CartolaMayorFuncionalDTO> oPresenter = new ManttoPresenter<CartolaMayorFuncionalDTO>();
             oPresenter.Initialize(this);
             oPresenter.RenderView(parentControl);
          }
          else if (sView == "CuentaCorriente")
          {
             ManttoPresenter<CuentaCorrienteFuncionalDTO> oPresenter = new ManttoPresenter<CuentaCorrienteFuncionalDTO>();
             oPresenter.Initialize(this);
             oPresenter.RenderView(parentControl);
          }
          else if (sView == "Egreso")
          {
             ManttoPresenter<EgresoFuncionalDTO> oPresenter = new ManttoPresenter<EgresoFuncionalDTO>();
             oPresenter.Initialize(this);
             oPresenter.RenderView(parentControl);
          }
          else if (sView == "Ingreso")
          {
             ManttoPresenter<IngresoFuncionalDTO> oPresenter = new ManttoPresenter<IngresoFuncionalDTO>();
             oPresenter.Initialize(this);
             oPresenter.RenderView(parentControl);
          }
          else if (sView == "Sucursal")
          {
             ManttoPresenter<SucursalFuncionalDTO> oPresenter = new ManttoPresenter<SucursalFuncionalDTO>();
             oPresenter.Initialize(this);
             oPresenter.RenderView(parentControl);
          }
        }
    }
}
